package com.ebei.platform.controller;

import com.ebei.platform.common.Constants;
import com.ebei.platform.common.StatusCode;
import com.ebei.platform.common.StatusCodeDesc;
import com.ebei.platform.common.util.RedisUtils;
import com.ebei.platform.entity.User;
import com.ebei.platform.vo.ResultVO;
import com.ebei.platform.vo.UserParams;
import com.ebei.platform.vo.UserPassword;

import io.swagger.annotations.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.plugins.Page;
import com.ebei.platform.service.IUserService;

/**
 * 用户Controller
 *
 * @author Victor.You@ebeitech
 * @version 1.0
 * @created 2018/6/1
 */

@RestController
@RequestMapping("/user")
@Api(value = "用户")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private RedisUtils redisUtils;
    
    @ApiOperation(value="获取用户分页数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "size", value = "URL参数：每页显示数量", required = true),
            @ApiImplicitParam(name = "current", value = "URL参数：当前页", required = true)
    })
    @PostMapping("/page/{size}/{current}")
    public ResultVO<Page<User>> page(@PathVariable int size, @PathVariable int current, @RequestBody @ApiParam(name="param",value="用户搜索条件JSON参数，无参请传{}",required=true) UserParams params) {
        return new ResultVO<>(StatusCode.SUCCESS, StatusCodeDesc.SUCCESS_DESC, userService.selectPage(new Page<>(current, size), params));
    }
    
    @ApiOperation(value = "按照用户ID获取用户详情", notes = "获取用户的详细信息")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/byId/{userId}")
    public ResultVO<User> getByUserId(@PathVariable String userId) {
        User user = userService.selectById(userId);
        return new ResultVO<User>(StatusCode.SUCCESS,StatusCodeDesc.SUCCESS_DESC, user);
    }
    
    
    @ApiOperation(value = "根据用户帐号获取用户详情", notes = "获取用户的详细信息")
    @ApiImplicitParam(name = "account", value = "用户帐号", required = true, dataType = "String", paramType = "path")
    @GetMapping("/byAccount/{account}")
    public ResultVO<User> getByUserAccount(@PathVariable String account) {
        User user = userService.selectByAccount(account);
        return new ResultVO<User>(StatusCode.SUCCESS,StatusCodeDesc.SUCCESS_DESC, user);
    }
    
    @ApiOperation(value="新增User")
    @PostMapping("/add")
    public ResultVO<Integer> addOne(@RequestBody @ApiParam(name="param",value="用户信息JSON",required=true) UserParams userParams, @RequestHeader HttpHeaders headers) {
        String userAccount = headers.getFirst(Constants.USER_HEADER);
        User operator = userService.selectByAccount(userAccount);
        User newUser = new User(userParams);
        int result = userService.insertUser(newUser, operator);
        if (result == -1) {
            return new ResultVO<>(StatusCode.BUSINESS_ERROR, "传入参数有误");
        }
        if (result == 0) {
            return new ResultVO<>(StatusCode.ERROR, "添加用户失败");
        }
        if (result == -2) {
            return new ResultVO<>(StatusCode.BUSINESS_ERROR, "用户已经存在");
        }
        return new ResultVO<>(StatusCode.SUCCESS, StatusCodeDesc.SUCCESS_DESC);
    }
    
    @ApiOperation(value="编辑User")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "URL参数：用户ID", required = true)
    })
    @PostMapping("/edit/{userId}")
    public ResultVO<Integer> editOne(@RequestBody @ApiParam(name="param",value="用户信息JSON",required=true) UserParams userParams, @PathVariable String userId, @RequestHeader HttpHeaders headers) {
        String userAccount = headers.getFirst(Constants.USER_HEADER);
        User user = new User(userParams);
        user.setId(userId);
        User operator = userService.selectByAccount(userAccount);
        int result = userService.updateUser(user, operator);
        if (result == -1) {
            return new ResultVO<>(StatusCode.BUSINESS_ERROR, "未找到用户");
        }
        if (result == 0) {
            return new ResultVO<>(StatusCode.ERROR, "修改用户信息失败");
        }
        return new ResultVO<>(StatusCode.SUCCESS, StatusCodeDesc.SUCCESS_DESC);
    }
    
    @ApiOperation(value="修改密码")
    @PostMapping("/pwd")
    public ResultVO<Integer> changePwd(@RequestBody UserPassword userPassword, @RequestHeader HttpHeaders headers) {
        String userAccount = headers.getFirst(Constants.USER_HEADER);
        User operator = userService.selectByAccount(userAccount);
        int result = userService.changePwd(userPassword, operator);
        if (result == -2) {
            return new ResultVO<>(StatusCode.BUSINESS_ERROR, "旧密码不匹配，修改失败");
        }
        if (result == -1) {
            return new ResultVO<>(StatusCode.BUSINESS_ERROR,"未找到用户");
        }
        if (result == 0) {
            return new ResultVO<>(StatusCode.ERROR, "修改密码失败");
        }
        return new ResultVO<>(StatusCode.SUCCESS, StatusCodeDesc.SUCCESS_DESC, result);
    }
    
}
